﻿package svl.gui
{
	import svl.gui.ButtonDisplayState;
	
	import flash.display.SimpleButton;
	import flash.filters.BevelFilter;
	import flash.filters.ColorMatrixFilter;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;	

	public class RoundedSimpleButton extends SimpleButton
	{
		
		private var locale:String;
		private var color:uint;
		private var margin:Number;
		private var shadow:Boolean;
		private var labelText:String;
		private var states:Array;
		
		private var newHeight:Number = 28;
		
		private var fillColor1:uint = 0x000099;
		private var fillColor2:uint = 0x000000;
		private var borderColor:uint = 0x3e69b1;
		private var hightLiteColor:uint = 0xffffff;
		
		private var font:String = "Arial";
		private var fontSize:int;
		
		public function RoundedSimpleButton(nlocale:String, nsize:int = 16, ntextcolor:uint = 0xffffff, nmargin:Number = 10, ntext:String = " ", nshadow: Boolean = true )
		{
			if (nlocale && nlocale != "")
			{
				locale = nlocale;
				fontSize = nsize;
				color = ntextcolor;
				margin = nmargin;
				labelText = ntext;
				shadow = nshadow;
				
				setup();
			}
		}
		

		private function setup():void
		{
			// shared border filter
			var borderfilter: GlowFilter = new GlowFilter(borderColor,1,3,3,6,2,true);
			
			// setup state filters
			var upStateFilters:Array = [borderfilter,
										new BevelFilter(newHeight/2, 90, hightLiteColor,0.6, 0x000000,0.6, 0,newHeight/2, 1, 2)];
										
			var overStateFilters:Array = [borderfilter,
										  new BevelFilter(newHeight/2, 90, hightLiteColor,0.6, 0x000000,0.6, 0,newHeight/2, 1, 2),
										  new ColorMatrixFilter([1.2,0,0,0,10, 0,1.2,0,0,10, 0,0,1.2,0,10, 0,0,0,1,0])];
											
			var downStateFilters:Array = [borderfilter,
										  new BevelFilter(newHeight/2, 90, hightLiteColor,0.6, 0x000000,0.6, 0,newHeight/2, 1, 2)];

			// optionally add shadow filter
			if (shadow)
			{
				var shadowfilter: GlowFilter  = new GlowFilter(0x000000,0.8,14,14,1,2)
				upStateFilters.push(shadowfilter)
				overStateFilters.push(shadowfilter)
				downStateFilters.push(shadowfilter)
			}
			
			states = [new ButtonDisplayState(createTextField(), [fillColor1, fillColor2], newHeight, margin, 13),
					  new ButtonDisplayState(createTextField(), [fillColor1, fillColor2], newHeight, margin, 13),
					  new ButtonDisplayState(createTextField(), [fillColor1, fillColor2], newHeight, margin, 13)];
			
			upState = states[0];
			overState = states[1];
			downState = states[2];
			hitTestState = states[0];
			
			upState.filters = upStateFilters;
			overState.filters = overStateFilters;
			downState.filters = downStateFilters;
		}
		
		private function createTextField():TextField
		{
			var tf:TextFormat = new TextFormat();
			tf.font = font;
			tf.size = fontSize;
			tf.color = color;
			
			var label:TextField = new TextField();
			label.autoSize = TextFieldAutoSize.LEFT;
			label.x = margin;
			label.y = newHeight/2 - label.height/2 - 1;
			label.defaultTextFormat = tf;
			return label;
		}
		
		public function set text(value:String):void
		{
			for each(var state:ButtonDisplayState in states)
			{
				state.text = value;
			}
		}
	}
}